#include <stdio.h>
#include <stdlib.h>
#include<string.h>
/*
    顺序表-结构体（相当于数组）
1.定义结构体 (结构体里面含有这个线性表的大小(size)，索引(last)，指向结构体数组的指针(p))

2.定义顺序表结构体(这里面是线性表的成员)

3.初始化顺序表（需要给结构体分配堆空间，给结构体数组分配堆空间）
4.尾插
5.打印顺序表



*/

typedef struct student student_t;
typedef struct student* pstudent_t;

struct student
{
    char name[20];
    char sex;
    int height;
    float score;

};

struct seq_list {
    int size;
    int last;
    pstudent_t p;
};

struct seq_list* init_list(int size)
{
    struct seq_list* list = (struct seq_list*)malloc(sizeof(struct seq_list));
    if (list == NULL)
    {
        perror("malloc seq_list fail");
        return NULL;
    }

    list->size = size;
    list->last = -1;
    list->p = (pstudent_t)malloc(sizeof(student_t) * size);
    if (list->p == NULL)
    {
        perror("malloc student fail");
        return NULL;
    }

    return list;
}

//尾插
void tail_insert(struct seq_list* list,char name[],char sex,int height,float score) {

    //判断表空,插入第一个值
    if (list->last == -1)
    {   list->last++;
        strcpy(list->p[list->last].name, name);
        list->p[list->last].sex = sex;
        list->p[list->last].height = height;
        list->p[list->last].score = score;
        
    }
   

    if (list->size != list->last + 1)
    {   list->last++;
        strcpy(list->p[list->last].name, name);
        list->p[list->last].sex = sex;
        list->p[list->last].height = height;
        list->p[list->last].score = score;
        
    }
    //判断表满
    else return ;

}

void print_list(struct seq_list *list) {
    for (int i = 0; i <= list->last; i++) {
        printf("名字:%s,性别:%c,身高:%d,分数:%f\n", list->p[i].name, list->p[i].sex,list->p[i].height,list->p[i].score);
    }

}

int main() {

    struct seq_list* list = init_list(5);
    tail_insert(list, "小明", 'm', 180, 100);
    tail_insert(list, "小红", 'w', 160, 90);
    tail_insert(list, "小灰", 'm', 170, 80);
    print_list(list);
    free(list);
    return 0;
}